home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 August / August CD.bin / Shareware / Education / numericalmethods Folder / chap_8 / a8_2.m < prev    next >
Encoding:
Text File  |  1994-06-05  |  3.3 KB  |  129 lines  |  [MATS/MATL]

  1. echo off;
  2. % NUMERICAL METHODS: MATLAB Programs, (c) John H. Mathews 1994
  3. % To accompany the text:
  4. % NUMERICAL METHODS for Mathematics, Science and Engineering, 2nd Ed, 1992
  5. % Prentice Hall, Englewood Cliffs, New Jersey, 07632, U.S.A.
  6. % This free software is complements of the author.
  7.  
  8. % Algorithm 8.2 (Nelder-Mead's Minimization Method).
  9. % Section    8.1,    Minimization of a Function, Page 414
  10. echo on; clc; format short; hold off; clear
  11.  
  12. % This program finds the minimum of a function Fn(X),
  13. % where X is an n-dimensional vector.
  14. % The technique is the Nelder-Mead method for n-dimensions.
  15. % The function  Fn(X)  is to be placed in two M-files 
  16. % The function f.m is for graphing and Fn.m is for computing.
  17.  
  18. % function z = f(x,y)
  19. % z = x.^2 - 4.*x + y.^2 - y - x.*y;
  20.  
  21. % function z = Fn(X)
  22. % x = X(1); y = X(2);
  23. % z = x^2 - 4*x + y^2 - y - x*y;
  24.  
  25. delete f.m
  26. diary f.m; disp('function z = f(x,y)');...
  27.            disp('z = x.^2 - 4.*x + y.^2 - y - x.*y;');...
  28. diary off;
  29.  
  30. delete Fn.m
  31. diary Fn.m; disp('function z = Fn(X)');...
  32.             disp('x = X(1); y = X(2);');...
  33.             disp('z = x^2 - 4*x + y^2 - y - x*y;');...
  34. diary off;
  35.  
  36. % Remark. f.m and Fn.m nelder.m are used for Algorithm 8.2
  37. % Remark. f(x,y) is used only to graph 2-dimensional functions.
  38. f(0,0); Fn([0 0]);
  39. pause    % Press any key to continue.
  40.  
  41. clc;
  42. % The example is 2-dimensional, hence a surface z = f(x,y)
  43.  
  44. % can be graphed.  (Omit this section for higher dimensions.)
  45.  
  46. % The endpoints of the rectangle [a,b] x [c,d] are a,b,c,d.
  47.  
  48. % Place the minimum and maximum number of iterations in
  49.  
  50. % min1 and max1, respectively.    
  51.  
  52. % Place the tolerance in  epsilon.
  53.  
  54. a  = 0;
  55. b  = 4;
  56. c  = 0;
  57. d  = 3;
  58. min1 = 8;
  59. max1 = 80;
  60. epsilon = 1e-6;
  61.  
  62. pause    % Press any key to graph the 2-dim function.
  63.  
  64. clc; clg;
  65. hs = (b-a)/16;
  66. ks = (d-c)/16;
  67. [Xs,Ys] = meshdom(a:hs:b, c:ks:d);
  68. Zs = f(Xs,Ys);
  69. mesh(Zs);...
  70. title('To search for the minimum of z = f(x,y).');...
  71. shg; pause    % Press any key to enter the starting vertices.
  72.  
  73. clc;
  74.  
  75. % The Nelder-Mead simplex method or "polytope method is used to
  76.  
  77. % find the minimum of the n-dim function Fn(X), for convenience,
  78.  
  79. % Fn(X) can be expressed using  X = (x1,x2,x3,x4,x5,x6)  and the
  80.   
  81. % variables x = x1 , y = x2 , z = x3 , u = x4 , v = x5 , w = x6 
  82.  
  83. % You must supply  n+1 linearly independent starting points:
  84.     
  85. % For n=2, supply: V(1,1:n), V(2,1:n), V(3,1:n)
  86.  
  87. % For n=3, supply: V(1,1:n), V(2,1:n), V(3,1:n), V(4,1:n)
  88.  
  89. n = 2;
  90. V(1,1:n) = [0.0   0.0];
  91. V(2,1:n) = [1.2   0.0];
  92. V(3,1:n) = [0.0   0.8];
  93.  
  94. pause % Press any key to continue.
  95.  
  96. clc; clg;
  97. a0  = 0;
  98. b0  = 4;
  99. c0  = 0;
  100. d0  = 3;
  101. hold off;
  102. XS = V(1:n+1,1)'; XSL = [XS,XS(1)];
  103. YS = V(1:n+1,2)'; YSL = [YS,YS(1)];
  104. axis([a0 b0 c0 d0]);...
  105. plot(XS,YS,'or',XSL,YSL,'-g');...
  106. hold on;...
  107. plot([a0 b0],[0 0],'b',[0 0],[c0 d0],'b');...
  108. xlabel('x');...
  109. ylabel('y');...
  110. title('The simplex vertices for Nelder-Mead`s method.');...
  111. grid;...
  112. shg; pause    % Press any key to continue.
  113.  
  114. [V0,y0,dV,dy,P,Q] = nelder('Fn',V,min1,max1,epsilon,1);
  115.  
  116. pause    % Press any key to find the minimum of Fn(X).
  117.  
  118. clc;
  119. format long;
  120. Mx1 = 'The coordinates of the point P are:';
  121. Mx2 = 'Error bound for the coordinates of  P:';
  122. Mx3 = 'The minimum value  F(P)  is:';
  123. Mx4 = 'Error bound for  F(P)  is:';
  124. clc,echo off,diary output,...
  125. disp(''),disp(Mx1),disp(V0),disp(Mx2),...
  126. disp(['± ',num2str(dV),'      ± ',num2str(dV)]),disp(''),...
  127. disp(Mx3),disp(y0),disp(Mx4),disp(['± ',num2str(dy)]),...
  128. diary off,echo on
  129.